﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class ReadDataSource
    {
        private ReadDataServiceContext _ServiceContext = null;

        public ReadDataSource(CloudStorageAccount storageAccount)
        {
            //var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new ReadDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(ReadDataServiceContext.ReadTableName);
        }

        public IEnumerable<ReadDataModel> Select()
        {
            var results = from c in _ServiceContext.ReadTable
                          select c;

            var query = results.AsTableServiceQuery<ReadDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<ReadDataModel> Select(string postid, string userid)
        //{
        //    var results = from c in _ServiceContext.ReadTable
        //                  where c.PostID==postid
        //                  where c.UserID==userid
        //                  select c;

        //    var query = results.AsTableServiceQuery<ReadDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}


        //public void Delete(ReadDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(ReadDataServiceContext.ReadTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}

        //public void Insert(ReadDataModel newItem)
        //{
        //    _ServiceContext.AddObject(ReadDataServiceContext.ReadTableName, newItem);
        //    _ServiceContext.SaveChanges();
        //}
    }
}
